cmake_minimum_required(VERSION 3.15)

project(ch5
  VERSION 0.0.1
  LANGUAGES CXX
)


set(CMAKE_CXX_STANDARD 20)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
  set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
list(APPEND CMAKE_PREFIX_PATH "${PROJECT_BINARY_DIR}")

find_package(Microsoft.GSL CONFIG REQUIRED)

include (CommonCompileFlags)

add_library(apis INTERFACE)
target_include_directories(apis INTERFACE src/apis)

# add_library(declarative src/apis/declarative.cpp)
add_executable(declarative src/apis/declarative.cpp)
target_compile_features(declarative PUBLIC cxx_std_20)
# end

add_executable(constexpr src/constexpr.cpp)
target_compile_features(constexpr PRIVATE cxx_std_20)
# target_compile_options(constexpr PRIVATE ${BASE_COMPILE_FLAGS})
#end

# add_executable(base src/1_base.cpp)
# target_compile_features(base PRIVATE cxx_std_20)
# target_link_libraries(base PRIVATE Microsoft.GSL::GSL)
# # target_compile_options(base PRIVATE ${BASE_COMPILE_FLAGS})
#end

add_executable(ranges1 src/2_ranges_1.cpp)
target_compile_features(ranges1 PRIVATE cxx_std_20)
target_link_libraries(ranges1 PRIVATE Microsoft.GSL::GSL)
# target_compile_options(ranges1 PRIVATE ${BASE_COMPILE_FLAGS})
#end

add_executable(ranges2 src/3_ranges_2.cpp)
target_link_libraries(ranges2 PRIVATE Microsoft.GSL::GSL)
# target_compile_options(ranges2 PRIVATE ${BASE_COMPILE_FLAGS})
target_compile_features(ranges2 PRIVATE cxx_std_20)
#end

add_executable(concepts src/4_concepts.cpp)
target_link_libraries(concepts PRIVATE Microsoft.GSL::GSL)
target_compile_features(concepts PRIVATE cxx_std_20)
# target_compile_options(concepts PRIVATE ${BASE_COMPILE_FLAGS})

option(ENABLE_MODULES "Compile the module example -- default off" OFF)

# NOW ENABLE this let OFF
if(ENABLE_MODULES)
  #this is workaround until cmake supports modules properly
  function(add_module_executable name) #then sources in specific order
    add_custom_target(
      ${name} ALL
      COMMAND
        g++ -std=c++20 -fmodules-ts
        ${CMAKE_CXX_FLAGS} #TODO: append build type specific compile flags and
                           # ${BASE_COMPILE_FLAGS}
                           ${ARGN} -o ${CMAKE_CURRENT_BINARY_DIR}/${name} -isystem
        ${PROJECT_SOURCE_DIR}/3rd-party/
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     )
  endfunction()

  add_module_executable(
    5_modules_exe 
    src/5_modules/merchant.cpp
    src/5_modules/customer.cpp
    src/5_modules/store.cpp
    src/5_modules/main.cpp
  )

endif()
